<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="80%"
  >
    
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button
            v-if="permissions.goods_spec_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column
            type="index"
            header-align="center"
            align="center"
            label="编号"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="详情名称"
          >
          </el-table-column>
          <el-table-column
            prop="sort"
            header-align="center"
            align="center"
            :show-overflow-tooltip="true"
            label="排序"
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            label="创建时间"
          >
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                v-if="permissions.goods_spec_edit"
                type="text"
                size="small"
                icon="el-icon-edit"
                @click="addOrUpdateHandle(scope.row.id)"
                >修改</el-button
              >
              <el-button
                v-if="permissions.goods_spec_del"
                type="text"
                size="small"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <EditForm
        v-if="attributeValueVisible"
        ref="addOrUpdate"
        @refreshDetailDataList="getDataList"
        :visible.sync="attributeValueVisible"
      ></EditForm>
  
  </el-dialog>
</template>

<script>
import { getGoodsSpecList, delObj } from "@/api/shop/goods/spec";
import EditForm from "@/views/shop/goods/spec_value/edit.vue";
import { mapGetters } from "vuex";
export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dataForm: {
        key: "",
      },
      queryParams: {
        specId:''
      },
      dataList: [],

      dataListLoading: false,
      attributeValueVisible: false,
    };
  },
  components: {
    EditForm,
  },
  // created() {
  //   this.getDataList();
  // },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {

    /**
     * 初始化
     */
    init(specId){
      this.queryParams.specId = specId
      this.getDataList()
    },
    /**
     * 获取数据列表
     */
    getDataList() {
      this.dataListLoading = true;
      getGoodsSpecList(this.queryParams).then((response) => {
        this.dataListLoading = false;
        this.dataList = response.data.data;
      });
    },

    /**
     * 新增 / 修改
     */
    addOrUpdateHandle(id) {
      this.attributeValueVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(this.queryParams.specId, id);
      });
    },

    /**
     * 删除
     */
    deleteHandle(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getDataList();
        })
        .catch(() => {});
    },

    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },

    indexMethod(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },
  },
};
</script>